package v3.object.command.list;

import galaxy.obj.Point;

import java.util.ArrayList;
import java.util.Map;

import text.TextUtil;
import v3.object.STcfg;
import v3.object.command.aCommand;
import v3.object.command.iCommand;
import v3.object.command.servoCommand.ComputerMessage;
import v3.object.command.servoCommand.Confirmation;
import v3.object.ship.Allert;
import v3.object.ship.Ship;
import v3.object.space.Universe;
import v3.object.space.iSpaceObj;

public class Impulse extends aCommand
{
	public Impulse()
	{
		super();
		commandName = "IMPULSE ENGINE";
	}

	Point oldPos;
	double d;
	double e;
	double t;
	Point newPos;
	int cursor = 0;
	double radius = 0;

	@Override
	public boolean realExecute(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{
		
		if(!universe.contains(newPos))
		{
			iCommand msg = new ComputerMessage(commandName+": ABORTED - we can not leave the known Universe, sorry");
			msg.execute(cfg, universe, ship, commands, input);
			return false;
		}
		
		//ArrayList<iSpaceObj> list = universe.getSector(newPos).getList();
		ship.setPosition(newPos);
		
		
		Point nsp = universe.getSector(newPos).getFreeSectorPoint();
		if(nsp==null)
		{
			iCommand msg = new ComputerMessage(commandName+": ABORTED - Fluctuation of the Dilithium Crystal stopped the Warp Drives");
			msg.execute(cfg, universe, ship, commands, input);
			ship.setPosition(oldPos);
		}
		ship.setSectorPosition(nsp);
	
		try
		{
			universe.getSector(newPos).setISpaceObj(nsp, ship);
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		iCommand ss = new ShortSensor();
		ss.execute(cfg, universe, ship, commands, input);
		return true;
		
//		iSpaceObj o = universe.getSpaceObject(newPos);
//		iCommand c=null;
//		if (o.getType().equals(SpaceObjType.Enemy))
//		{
//			// attack
//			c = new ComputerMessage("SNEAK ATTAK! - ****");
//		}
//		else if (o.getType().equals(SpaceObjType.Star))
//		{
//			// Orbit
//			c = new ComputerMessage("SolarSistem Scan - ****");
//		}
//		else if (o.getType().equals(SpaceObjType.StarBase))
//		{
//			// Dock
//			c = new ComputerMessage("StareBase DOCK - ****");
//		}
//		if(c!=null)
//			c.execute(cfg, universe, ship, commands, input);
//		
//		ship.setPosition(newPos);
//		Point newQ = ship.getQuadrant();
//
//		if (q.equals(newQ))
//		{
//			// iCommand conf = new
//			// Confirmation(commandName+": INFO - distance:",
//			// format_decimal.format(d)+""," Energy: ",
//			// format_decimal.format(e)+"" , " Time: ",
//			// format_decimal.format(t)+"");
//			// if (!conf.execute(cfg, universe, ship, commands, input))
//			// {
//			// ship.setPosition(oldPos);
//			// iCommand msg = new ComputerMessage(commandName+": CANCELLED");
//			// msg.execute(cfg, universe, ship, commands, input);
//			// return false;
//			// }
//			ship.energy = ship.energy - e;
//			universe.starDate = universe.starDate + t;
//
//			iCommand msg = new ComputerMessage(commandName + ": EXECUTED - distance:", format_decimal.format(d) + "", " Energy: ", format_decimal.format(e) + "", " Time: ", format_decimal.format(t)
//					+ "");
//			msg.execute(cfg, universe, ship, commands, input);
//			c = new ShortSensor();
//			c.execute(cfg, universe, ship, commands, input);
//		}
//		else
//		{
//			ship.setPosition(p);
//			iCommand msg = new ComputerMessage(commandName + ": ABORTED - Leave the quadrant Require Warp Drive");
//			msg.execute(cfg, universe, ship, commands, input);
//
//			return false;
//		}

	}

	@Override
	public void getHelp(Ship ship)
	{
		String help = commandName + " [i] \n\n" + "Format: Cursor Radius\n" + "Allow to move the Ship throgh space at subLight speed, \n" + "it is necessary indicate the Cursor: "
				+ "and the Lenght of the path\n" + "Time Cost   : "+ship.getTimeConsume((this.getClass()))+" day/Sector\n" + "Energy Cost : "+ship.getEnergyConsume((this.getClass()))+" unit/Sector";
		help = TextUtil.formatHelp(help);
		System.out.println(help);

	}

	@Override
	protected double calculateEnergy(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{
		oldPos = ship.getPosition();
		// double y = cfg.cos(cursor) * radius;
		// double x = -cfg.sin(cursor) * radius;
		// Point vector = new Point(x, y, 0);
		// newPos = p.add(vector);
		newPos = STcfg.getNewPosition(ship.getPosition(), cursor, radius);
		d = oldPos.distance(newPos);
		e = d * ship.getEnergyConsume(this.getClass());
		t = d * ship.getTimeConsume(this.getClass());
		return e;
	}

	@Override
	protected void checkInput(String[] input) throws Exception
	{
		cursor = Integer.parseInt(input[1]);
		radius = Double.parseDouble(input[2]);
	}

	@Override
	protected boolean isCommandAccetable(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{
		if (ship.allert.equals(Allert.RED))
		{
			iCommand c = new ComputerMessage("Under RED IMPULSE command is not available, the ship must DISENGAGE before");
			c.execute(cfg, universe, ship, commands, input);
			return false;
		}
		return true;

	}

	@Override
	protected double calculateTime(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{
		return ship.getTimeConsume((this.getClass()));
	}
	
	@Override
	protected boolean confirmCommand(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{
		
		iCommand conf = new Confirmation(commandName+": INFO","\n\ndistance:\t",
		format_decimal.format(d)+"","\nEnergy:  \t",
		format_decimal.format(e)+"" , "\nTime:   \t",
		format_decimal.format(t)+"\n");
		return conf.execute(cfg, universe, ship, commands, input);
	}
}
